function getUserIP() { // Check if the 'HTTP_X_FORWARDED_FOR' header exists (used when behind a proxy or load balancer) if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { // This may contain multiple IPs, so split by comma and get the first one $ip = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])[0]; } // If the above header doesn't exist, fall back to 'REMOTE_ADDR' elseif (!empty($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } else { $ip = $_SERVER['REMOTE_ADDR']; } return $ip; }